/// ====================市场配置website_setting.js


var selectId = "Select8";
var imagesId = "Upload5";

var goodsId = ['Input_goods0',
    'Input_goods1',
    'Input_goods2',
    'Input_goods3',
    'Input_goods4',
    'Input_goods5',
    'Input_goods6',
    'Input_goods7'];
var descriptionId = "goods_description";


//初始化
function initstart() {
    console.log(12321);


    //加载轮播图
    initCarouselcomp();

    modelLoad();

}

// 加载轮播图
function initCarouselcomp() {
    var pageIndex = 0;
    var pageSize = 10;
    var filter = {position: "商城", type: "轮播图"};
    h3ui.models.t_website_setting.list({pageIndex, pageSize, filter}).then(res => {

        if (res.total > 0) {
            h3ui.models.t_website_setting.item = res.items[0];

            if (h3ui.models.t_website_setting.item.image_url) {
                h3ui.models.t_website_setting.item.image_url.forEach(b => {
                    b.uid = b.id;
                    b.status = "done";
                    b.url = "/api/file/download?refId=" + b.id;
                })
            }


        }

    }).catch(res => {
        h3ui.$message.info("轮播图数据初始化失败:" + res);
    });


}

// 加载模块
function modelLoad() {
    var pageIndex = 0;
    var pageSize = 10;
    var filter = {position: "商城", type: "分类楼层"};

    h3ui.models.t_website_setting.list({pageIndex, pageSize, filter}).then(res => {
        modelhandel(res.items);
    }).catch(res => {
        h3ui.$message.info("轮播图数据初始化失败:" + res);
    });
}


// 加载模块组数据
function modelhandel(list) {
    var selectProps = h3ui.widgets[selectId].props;
    if (list == null || list.length == 0) {
        var image_url = [];
        var contents = goodsId.map(a => "").join();
        var description = null;
        list = [];
        var position = "商城";
        var id = null;

        selectProps.options.forEach((a, index) => {
            var type = "分类楼层".concat(index + 1);
            list[a.value] = {image_url, contents, description, id, type, position, id}
        });

    } else {
        //排序
        var sortCompare = (a, b) => {
            var x = a.type.split("分类楼层")[1];
            var y = b.type.split("分类楼层")[1];
            return x - y;
        }

        list.sort(sortCompare);
    }

    list.forEach(a => {
        a.contents = a.contents.split(",");
        if (a.image_url) {
            a.image_url.forEach(b => {
                b.uid = b.id;
                b.status = "done";
                b.url = "/api/file/download?refId=" + b.id;
            })
        }


    });


    selectProps.modelList = list;
    selectProps.modelIndex = null;
    h3ui.widgets.Select8.props.value = "0"
    var evt = {data: {value: 0}};
    toSelected(evt);
}

//选择下拉
function toSelected(evt) {
    console.log(evt);

    var selectProps = h3ui.widgets[selectId].props;
    var list = selectProps.modelList;
    var modelIndex = selectProps.modelIndex;

    var index = evt.data.value;

    var image_url = [];
    var contents = null;
    var description = null;

    //缓存当前模块数据
    if (modelIndex != null) {
        var data = list[modelIndex];
        data.image_url = h3ui.widgets[imagesId].props.value;
        data.contents = goodsId.map(a => h3ui.widgets[a].props.value);
        data.description = h3ui.widgets[descriptionId].props.value;
    }

    //给切换后的模块赋值
    if (index == "$CLEAR") {
        h3ui.widgets[imagesId].props.value = [];
        goodsId.forEach(a => h3ui.widgets[a].props.value = "");
        h3ui.widgets[descriptionId].props.value = "";

        selectProps.modelIndex = null;
    } else {
        //记录checkIndex,方便下次check 前保存当前填写的缓存
        selectProps.modelIndex = index;

        //赋值逻辑

        var data = list[index];
        h3ui.widgets[imagesId].props.value = data.image_url;
        goodsId.forEach((a, i) => h3ui.widgets[a].props.value = data.contents[i]);
        h3ui.widgets[descriptionId].props.value = data.description;
    }
}


//保存轮播图
function savecarouselcompId() {

    var dataSource = h3ui.models.t_website_setting;

    if (dataSource.item.id == null) {
        dataSource.item.position = "商城";
        dataSource.item.type = "轮播图";
        dataSource.item.description = "轮播图";
    }

    var id = dataSource.item.id;
    var position = dataSource.item.position;
    var type = dataSource.item.type;
    var description = dataSource.item.description;
    var image_url = dataSource.item.image_url;

    var data = {id, position, type, description, image_url};
    var schemaCode = dataSource.schema.code;

    dataSource.convert(data, dataSource.schema);

    dataSource.submitBO({data, schemaCode}).then(res => {
        dataSource.item.id = res.bizObjectId;
        h3ui.$message.info("保存成功");
    })

}

//保存模块
function saveModel(evt) {

    var selectProps = h3ui.widgets[selectId].props;
    var list = selectProps.modelList;
    var map = [];

    var modelIndex = selectProps.modelIndex;

    //缓存当前模块数据
    if (modelIndex != null) {
        var data = list[modelIndex];
        data.image_url = h3ui.widgets[imagesId].props.value;
        data.contents = goodsId.map(a => h3ui.widgets[a].props.value);
        data.description = h3ui.widgets[descriptionId].props.value;

    }
    var dataSource = h3ui.models.t_website_setting;
    list.forEach(a => {
        var type = a.type;
        var contents = a.contents.join();
        var description = a.description;
        var image_url = a.image_url;
        var id = a.id;
        var position = a.position;

        var data = {type, contents, description, image_url, position, id};
        var schemaCode = dataSource.schema.code;

        dataSource.convert(data, dataSource.schema);

        dataSource.submitBO({data, schemaCode}).then(res => {
            if (id == null) {
                a.id = res.bizObjectId;
            }
        })

        map.push(position);
    });
    h3ui.$message.info("保存成功");

}